//
//  UITableView+EmptyData.m
//  STLPro
//
//  Created by maia on 16/4/28.
//  Copyright © 2016年 XMYStudio. All rights reserved.
//

#import "UITableView+EmptyData.h"

@implementation UITableView (EmptyData)

- (void)tableViewDisplayWitMsg:(NSString *) message ifNecessaryForRowCount:(NSUInteger) rowCount
{
    if (rowCount == 0)
    {
        UIView * backView = [UIView new];
        [backView sizeToFit];
        self.backgroundView = backView;
        UIImageView * imgView = [UIImageView new];
        [imgView setImage:[UIImage imageNamed:@"nodate"]];
        [backView addSubview:imgView];
        imgView.frame = CGRectMake((SCREENWIDTH-75)/2, 136, 75, 58);
        
        UILabel *messageLabel = [UILabel new];
        messageLabel.text = message;
        messageLabel.font = FontWithSize(18);
        messageLabel.textColor = RGBACOLOR(204, 204, 204, 1);
        messageLabel.textAlignment = NSTextAlignmentCenter;
        [backView addSubview:messageLabel];
        messageLabel.frame = CGRectMake((SCREENWIDTH-180)/2, imgView.bottom+15, 180, 18);
        [messageLabel sizeToFit];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    } else {
        self.backgroundView = nil;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
}
- (void) tableViewMyCollection:(NSString *) message ifNecessaryForRowCount:(NSUInteger) rowCount typeName:(NSString *)type
{
    float height,width;
    
    if ([type isEqualToString:@"NaoNao"])
    {
        height = 38;
        width = 229;
    } 
    else
    {
        height = 41;
        width = 161;
    }
    if (rowCount == 0) {
        // Display a message when the table is empty
        // 没有数据的时候，UILabel的显示样式
        UIView * backView = [UIView new];
        [backView sizeToFit];
        self.backgroundView = backView;
        UIImageView * imgView = [UIImageView new];
        [imgView setImage:[UIImage imageNamed:message]];
        [backView addSubview:imgView];
        
        
        
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    } else {
        self.backgroundView = nil;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
}

- (void)tableViewDisplayWitMsg:(NSString *)message
        ifNecessaryForRowCount:(NSUInteger)rowCount
               withBottonTitle:(NSString *)btnTitle
{
    if (rowCount == 0)
    {
        UIView * backView = [UIView new];
        [backView sizeToFit];
        self.backgroundView = backView;
        UIImageView * imgView = [UIImageView new];
        [imgView setImage:[UIImage imageNamed:@"nodata_goods"]];
        [backView addSubview:imgView];
        imgView.frame = CGRectMake((SCREENWIDTH-75)/2, 136, 75, 70);
        
        UILabel *messageLabel = [UILabel new];
        messageLabel.text = message;
        messageLabel.font = FontWithSize(18);
        messageLabel.textColor = RGBACOLOR(204, 204, 204, 1);
        messageLabel.textAlignment = 1;
        [backView addSubview:messageLabel];
        messageLabel.frame = CGRectMake(0, imgView.bottom+15, SCREENWIDTH, 18);
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backView addSubview:btn];
        btn.frame = CGRectMake((SCREENWIDTH-100)/2, messageLabel.bottom+15, 100, 30);
        btn.layer.cornerRadius = 15;
        btn.layer.masksToBounds = YES;
        btn.backgroundColor = BtnBgColor;
        btn.titleLabel.font = FontWithSize(12);
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn setTitleColor:BlackTextColor forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    } else {
        self.backgroundView = nil;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
}

- (void)btnClick {
    if (self.myBlcok)
    {
        self.myBlcok(1);
    }
}

- (void)setMyBlcok:(BtnBlock)myBlcok {
    objc_setAssociatedObject(self, @"myBlcok", myBlcok, OBJC_ASSOCIATION_RETAIN);
}

- (BtnBlock)myBlcok
{
    return objc_getAssociatedObject(self, @"myBlcok");
}
@end
